"""
@author: 江同学呀
@file: query_common.py
@date: 2025/1/9 21:58
@desc: 
"""
from espc.utils.enum_utils import EnumBase


class RangeRelation(EnumBase):
    """
    (Optional, string) Indicates how the range query matches values for range fields. Valid values are:
    （可选，字符串）指示范围查询如何匹配范围字段的值。有效值为：
    """
    intersects = "INTERSECTS"
    """
    Matches documents with a range field value that intersects the query’s range.
    将文档与与查询范围相交的范围字段值匹配。
    """
    contains = "CONTAINS"
    """
    Matches documents with a range field value that entirely contains the query’s range.
    将文档与完全包含查询范围的 range 字段值匹配。
    """
    within = "WITHIN"
    """
    Matches documents with a range field value entirely within the query’s range.
    匹配 range 字段值完全在查询范围内的文档。
    """


class Rewrite(EnumBase):
    """
    Elasticsearch uses Apache Lucene internally to power indexing and searching. In their original form, Lucene cannot
    execute the following queries:
        - fuzzy
        - prefix
        - query_string
        - regexp
        - wildcard
    To execute them, Lucene changes these queries to a simpler form, such as a bool query or a bit set.
    The rewrite parameter determines:
        - How Lucene calculates the relevance scores for each matching document
        - Whether Lucene changes the original query to a bool query or bit set
        - If changed to a bool query, which term query clauses are included

    Elasticsearch 在内部使用 Apache Lucene 来支持索引和搜索。在原始形式中，Lucene 无法执行以下查询：
        - fuzzy
        - prefix
        - query_string
        - regexp
        - wildcard
    为了执行它们，Lucene 将这些查询更改为更简单的形式，例如 bool 查询或 bit set。
    rewrite 参数确定：
        - Lucene 如何计算每个匹配文档的相关性分数
        - Lucene 是否将原始查询更改为 bool 查询或位集
        - 如果更改为 bool 查询，则包含哪些 term 查询子句
    """
    constant_score = "constant_score"
    """
    Uses the constant_score_boolean method for fewer matching terms. Otherwise, this method finds all matching terms 
    in sequence and returns matching documents using a bit set.
    对较少的匹配词使用 constant_score_boolean 方法。否则，此方法将按顺序查找所有匹配的术语，并使用位集返回匹配的文档。
    """
    constant_score_boolean = "constant_score_boolean"
    """
    Assigns each document a relevance score equal to the boost parameter.
    This method changes the original query to a bool query. This bool query contains a should clause and term query 
    for each matching term.
    This method can cause the final bool query to exceed the clause limit in the indices.query.bool.max_clause_count 
    setting. If the query exceeds this limit, Elasticsearch returns an error.
    
    为每个文档分配一个等于 boost 参数的相关性分数。
    该方法将原始查询更改为 bool 查询。此 bool 查询包含每个匹配词的 should 子句和 term 查询。
    此方法可能导致最终的 bool 查询超过 indices.query.bool.max_clause_count 设置中的子句限制。如果查询超过此限制，Elasticsearch 将返回错误。
    """
    scoring_boolean = "scoring_boolean"
    """
    Calculates a relevance score for each matching document.
    This method changes the original query to a bool query. This bool query contains a should clause and term query for 
    each matching term.
    This method can cause the final bool query to exceed the clause limit in the indices.query.bool.max_clause_count 
    setting. If the query exceeds this limit, Elasticsearch returns an error.
    
    计算每个匹配文档的相关性分数。
    该方法将原始查询更改为 bool 查询。此 bool 查询包含每个匹配词的 should 子句和 term 查询。
    此方法可能导致最终的 bool 查询超过 indices.query.bool.max_clause_count 设置中的子句限制。如果查询超过此限制，Elasticsearch 将返回错误。
    """
    top_terms_blended_freqs_N = "top_terms_blended_freqs_N"
    """
    Calculates a relevance score for each matching document as if all terms had the same frequency. This frequency is 
    the maximum frequency of all matching terms.
    This method changes the original query to a bool query. This bool query contains a should clause and term query for 
    each matching term.
    The final bool query only includes term queries for the top N scoring terms.
    You can use this method to avoid exceeding the clause limit in the indices.query.bool.max_clause_count setting.

    计算每个匹配文档的相关性分数，就好像所有术语具有相同的频率一样。此频率是所有匹配词的最大频率。
    该方法将原始查询更改为 bool 查询。此 bool 查询包含每个匹配词的 should 子句和 term 查询。
    最终的 bool 查询仅包含得分前 N 个词的 term 查询。
    您可以使用此方法来避免超出 indices.query.bool.max_clause_count 设置中的子句限制。
    """
    top_terms_boost_N = "top_terms_boost_N"
    """
    Assigns each matching document a relevance score equal to the boost parameter.
    This method changes the original query to a bool query. This bool query contains a should clause and term query for 
    each matching term.
    The final bool query only includes term queries for the top N terms.
    You can use this method to avoid exceeding the clause limit in the indices.query.bool.max_clause_count setting.

    为每个匹配的文档分配一个等于 boost 参数的相关性分数。
    该方法将原始查询更改为 bool 查询。此 bool 查询包含每个匹配词的 should 子句和 term 查询。
    最终的 bool 查询仅包含前 N 个字词的字词查询。
    您可以使用此方法来避免超出 indices.query.bool.max_clause_count 设置中的子句限制。
    """
    top_terms_N = "top_terms_N"
    """
    Calculates a relevance score for each matching document.
    This method changes the original query to a bool query. This bool query contains a should clause and term query for 
    each matching term.
    The final bool query only includes term queries for the top N scoring terms.
    You can use this method to avoid exceeding the clause limit in the indices.query.bool.max_clause_count setting.

    计算每个匹配文档的相关性分数。    
    该方法将原始查询更改为 bool 查询。此 bool 查询包含每个匹配词的 should 子句和 term 查询。
    最终的 bool 查询仅包含得分前 N 个词的 term 查询。
    您可以使用此方法来避免超出 indices.query.bool.max_clause_count 设置中的子句限制。
    """


class ScoreMode(EnumBase):
    """
    评分模式
    """
    multiply = "multiply"
    """
    scores are multiplied (default)
    分数相乘 （默认）
    """
    sum = "sum"
    """
    scores are summed
    分数相加
    """
    avg = "avg"
    """
    scores are averaged
    分数是平均的
    """
    first = "first"
    """
    the first function that has a matching filter is applied
    应用具有匹配筛选器的第一个函数
    """
    max = "max"
    """
    maximum score is used
    使用最高分
    """
    min = "min"
    """
    minimum score is used
    使用最低分数
    """
    none = "none"
    """
    Do not use the relevance scores of matching child objects. The query assigns parent documents a score of 0.
    请勿使用匹配子对象的相关性分数。查询为父文档分配分数 0。
    """
    total = "total"
    """
    Add the original score and the rescore query score. The default.
    添加 original score 和 rescore 查询分数。默认值。
    """


class BoostMode(EnumBase):
    """
    Boosting模式
    """
    multiply = "multiply"
    """
    query score and function score is multiplied (default)
    查询分数和函数分数相乘（默认）
    """
    replace = "replace"
    """
    only function score is used, the query score is ignored
    仅使用函数 score，查询分数将被忽略
    """
    sum = "sum"
    """
    query score and function score are added
    添加了 Query Score 和 Function Score
    """
    avg = "avg"
    """
    average
    平均
    """
    max = "max"
    """
    max of query score and function score
    查询分数和函数分数的最大值
    """
    min = "min"
    """
    min of query score and function score
    查询分数和函数分数的最小值
    """


class Modifier(EnumBase):
    """
    Modifier

    Modifier to apply to the field value, can be one of: none, log, log1p, log2p, ln, ln1p, ln2p, square, sqrt, or reciprocal. Defaults to none.
    要应用于字段值的修饰符可以是以下值之一：none、log、log1p、log2p、ln、ln1p、ln2p、square、sqrt 或 reciprocal。默认为 none。
    """
    none = "none"
    """
    Do not apply any multiplier to the field value
    不对字段值应用任何乘数
    """
    log = "log"
    """
    Take the common logarithm of the field value. Because this function will return a negative value and cause an error 
    if used on values between 0 and 1, it is recommended to use log1p instead.
    取字段值的公共对数。由于此函数将返回负值，如果用于 0 到 1 之间的值，则会导致错误，因此建议改用 log1p。
    """
    log1p = "log1p"
    """
    Add 1 to the field value and take the common logarithm
    将字段值加 1 并取公共对数
    """
    log2p = "log2p"
    """
    Add 2 to the field value and take the common logarithm
    将字段值加 2 并取公共对数
    """
    ln = "ln"
    """
    Take the natural logarithm of the field value. Because this function will return a negative value and cause an 
    error if used on values between 0 and 1, it is recommended to use ln1p instead.
    取字段值的自然对数。由于此函数将返回负值，如果用于 0 到 1 之间的值，则会导致错误，因此建议改用 ln1p。
    """
    ln1p = "ln1p"
    """
    Add 1 to the field value and take the natural logarithm
    将字段值加 1 并取自然对数
    """
    ln2p = "ln2p"
    """
    Add 2 to the field value and take the natural logarithm
    将字段值加 2 并采用自然对数
    """
    square = "square"
    """
    Square the field value (multiply it by itself)
    将字段值平方（乘以自身）
    """
    sqrt = "sqrt"
    """
    Take the square root of the field value
    取字段值的平方根
    """
    reciprocal = "reciprocal"
    """
    Reciprocate the field value, same as 1/x where x is the field’s value
    倒数字段值，与 1/x 相同，其中 x 是字段的值
    """


class MultiValueMode(EnumBase):
    """
    多值字段模式

    If a field used for computing the decay contains multiple values, per default the value closest to the origin is
    chosen for determining the distance. This can be changed by setting multi_value_mode.
    如果用于计算衰减的字段包含多个值，则默认情况下，将选择最接近原点的值来确定距离。这可以通过设置 multi_value_mode 来更改。
    """
    min = "min"
    """
    Distance is the minimum distance
    Distance 是最小距离
    """
    max = "max"
    """
    Distance is the maximum distance
    Distance 是最大距离
    """
    avg = "avg"
    """
    Distance is the average distance
    Distance 是平均距离
    """
    sum = "sum"
    """
    Distance is the sum of all distances
    Distance 是所有距离的总和
    """


class ValidationMethod(EnumBase):
    """
    Set to IGNORE_MALFORMED to accept geo points with invalid latitude or longitude, set to COERCE to additionally try
    and infer correct coordinates (default is STRICT).
    设置为 IGNORE_MALFORMED 以接受具有无效纬度或经度的地理点，设置为 COERCE 以额外尝试推断正确的坐标（默认值为 STRICT）。
    """
    strict = "STRICT"
    ignore_malformed = "IGNORE_MALFORMED"
    coerce = "COERCE"


class DistanceType(EnumBase):
    """
    How to compute the distance. Can either be arc (default), or plane (faster, but inaccurate on long distances and
    close to the poles).
    如何计算距离。可以是 arc （默认） 或 plane （更快，但在长距离和靠近极点时不准确）。
    """
    arc = "arc"
    plane = "plane"


class SpatialRelation(EnumBase):
    """
    The geo_shape strategy mapping parameter determines which spatial relation operators may be used at search time.
    geo_shape 策略映射参数用于确定在搜索时可以使用哪些空间关系运算符。
    """
    intersects = "INTERSECTS"
    """
    (default) Return all documents whose geo_shape or geo_point field intersects the query geometry.
    （默认）返回 geo_shape 或 geo_point 字段与查询几何图形相交的所有文档。
    """
    disjoint = "DISJOINT"
    """
    Return all documents whose geo_shape or geo_point field has nothing in common with the query geometry.
    返回其 geo_shape 或 geo_point 字段与查询几何图形没有任何共同点的所有文档。
    """
    within = "WITHIN"
    """
    Return all documents whose geo_shape or geo_point field is within the query geometry. Line geometries are not supported.
    返回 geo_shape 或 geo_point 字段位于查询几何图形内的所有文档。不支持线几何图形。
    """
    contains = "CONTAINS"
    """
    Return all documents whose geo_shape or geo_point field contains the query geometry.
    返回 geo_shape 或 geo_point 字段包含查询几何图形的所有文档。
    """


class Operator(EnumBase):
    """
    (Optional, string) Boolean logic used to interpret text in the query value. Valid values are:
    （可选，字符串）用于解释查询值中的文本的布尔逻辑。有效值为：
    """
    and_ = "AND"
    """
    例如，查询 capital of Hungary 被解释为 capital 且 of 且 Hungary。
    """
    or_ = "OR"
    """
    例如，查询 capital of Hungary 被解释为 capital 或 of 或 Hungary。
    """


class ZeroTermsQuery(EnumBase):
    """
    (Optional, string) Indicates whether no documents are returned if the analyzer removes all tokens, such as when
    using a stop filter. Valid values are:
    （可选，字符串）指示如果分析器删除所有标记（例如使用停止筛选器时），是否不返回任何文档。有效值为：
    """
    none = "none"
    """
    No documents are returned if the analyzer removes all tokens.
    如果分析器删除所有标记，则不会返回任何文档。
    """
    all = "all"
    """
    Returns all documents, similar to a match_all query.
    返回所有文档，类似于 match_all 查询。
    """


class MultiMatchType(EnumBase):
    """
    The way the multi_match query is executed internally depends on the type parameter, which can be set to:
    multi_match查询在内部执行的方式取决于 type 参数，该参数可以设置为：
    """
    best_fields = "best_fields"
    """
    (default) Finds documents which match any field, but uses the _score from the best field. See best_fields.
    （默认）查找与任何字段匹配的文档，但使用最佳字段中的_score。请参阅 best_fields。
    """
    most_fields = "most_fields"
    """
    Finds documents which match any field and combines the _score from each field. See most_fields.
    查找与任何字段匹配的文档，并合并每个字段中的_score。请参阅 most_fields。
    """
    cross_fields = "cross_fields"
    """
    Treats fields with the same analyzer as though they were one big field. Looks for each word in any field. 
    See cross_fields.
    将具有相同分析器的字段视为一个大字段。在任何字段中查找每个单词。请参阅 cross_fields。
    """
    phrase = "phrase"
    """
    Runs a match_phrase query on each field and uses the _score from the best field. See phrase and phrase_prefix.
    对每个字段运行 match_phrase 查询，并使用 best 字段中的 _score。请参阅 phrase 和 phrase_prefix。
    """
    phrase_prefix = "phrase_prefix"
    """
    Runs a match_phrase_prefix query on each field and uses the _score from the best field. See phrase and phrase_prefix.
    对每个字段运行 match_phrase_prefix 查询，并使用最佳字段中的_score。请参阅 phrase 和 phrase_prefix。
    """
    bool_prefix = "bool_prefix"
    """
    Creates a match_bool_prefix query on each field and combines the _score from each field. See bool_prefix.
    在每个字段上创建一个 match_bool_prefix 查询，并合并每个字段中的_score。请参阅 bool_prefix。
    """


class OperatorFlag(EnumBase):
    """
    运算符标志
    """
    all = "ALL"
    """
    ALL (Default)
    Enables all optional operators.

    ALL （默认）
    启用所有可选运算符。
    """
    and_ = "AND"
    """
    Enables the + AND operator.
    启用 + AND 运算符。
    """
    escape = "ESCAPE"
    r"""
    Enables \ as an escape character.
    启用 \ 转义字符。
    """
    fuzzy = "FUZZY"
    """
    Enables the ~N operator after a word, where N is an integer denoting the allowed edit distance for matching. 
    See Fuzziness.
    在单词后启用 ~N 运算符，其中 N 是一个整数，表示允许的匹配编辑距离。请参阅模糊。
    """
    near = "NEAR"
    """
    Enables the ~N operator, after a phrase where N is the maximum number of positions allowed between matching tokens. 
    Synonymous to SLOP.
    在短语后启用 ~N 运算符，其中 N 是匹配标记之间允许的最大位置数。与 SLOP 同义。
    """
    none = "NONE"
    """
    Disables all operators. 
    禁用所有运算符。
    """
    not_ = "NOT"
    """
    Enables the - NOT operator.
    启用 - NOT 运算符。
    """
    or_ = "OR"
    r"""
    Enables the \| OR operator.
    启用 \|OR 运算符。
    """
    phrase = "PHRASE"
    """
    Enables the " quotes operator used to search for phrases.
    启用 “ quotes 运算符，用于搜索短语。
    """
    precedence = "PRECEDENCE"
    """
    Enables the ( and ) operators to control operator precedence.
    启用 （ 和 ） 运算符以控制运算符优先级。
    """
    prefix = "PREFIX"
    """
    Enables the * prefix operator.
    启用 * 前缀运算符。
    """
    slop = "SLOP"
    """
    Enables the ~N operator, after a phrase where N is maximum number of positions allowed between matching tokens. 
    Synonymous to NEAR.
    在短语后启用 ~N 运算符，其中 N 是匹配标记之间允许的最大位置数。与 NEAR 同义。
    """
    whitespace = "WHITESPACE"
    """
    Enables whitespace as split characters.
    启用空格作为拆分字符。
    """








